package com.steven.sync;

import java.util.concurrent.Exchanger;

/**
 * 线程之间数据交换
 * 游戏中交换装备
 */
public class Exchanger_test {
   static Exchanger exchanger =  new Exchanger<>();

    public static void main(String[] args) {
        new Thread(()->{
            Object name ="t1";
            try {
                 exchanger.exchange(name);
                System.out.println(Thread.currentThread().getName() +name);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }).start();

        new Thread(()->{
            Object name ="t2";
            try {
                 exchanger.exchange(name);
                System.out.println(Thread.currentThread().getName() +name);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

}
